/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kason.fixlength;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author 卢清波
 * @date 2021/2/3 16:47
 */
public class FixLengthClientHandler extends SimpleChannelInboundHandler<MyMessage>
{

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("连接服务端成功");
        MyMessage myMessage = new MyMessage();
        String content = "您好，服务端";
        for (int i=0;i<10;i++)
        {
            content = content + (i+1);
            myMessage.setLength(content.getBytes("UTF-8").length);
            myMessage.setContent(content.getBytes("UTF-8"));
            ctx.writeAndFlush(myMessage);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyMessage msg) throws Exception
    {
        System.out.println("收到服务端数据！");
    }
}
